package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.EventSignUpService;
import es.ats.codington.festival.services.PlaceService;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class PersonalServlet
 */
@WebServlet("/PersonalServlet")
public class PersonalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PersonalServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    /**
     * Method invoked when loading "personalvisitor.jsp"
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		int idVisitor = 0;
			
        
		HttpSession session = request.getSession(true);
		
		VisitorService vservice = new VisitorService();
		EventSignUp esu = new EventSignUp();
		EventService service3 = new EventService();
		EventSignUpService eservice = new EventSignUpService();
		List<Event> events = new ArrayList<Event>();
		List<Event> eventsRegistered = new ArrayList<Event>();
		List<Event> eventsNotRegistered = new ArrayList<Event>();
		
		/**
		 * Get all events
		 */
		events = service3.getAllEvents();
		
		Visitor v = new Visitor();
		String n=(String) session.getAttribute("username");
		v.setUserName(n);
		
		idVisitor = vservice.getIdVisitor(v);
		esu.setIdVisitor(idVisitor);
		/**
		 * Get events where the visitor is registered
		 */
		eventsRegistered = eservice.registeredEvents(esu);
		
		/*Iterator<Event> it1 = events.iterator();
		while(it1.hasNext()) {
			Event ev1 = it1.next();
			Iterator<Event> it2 = eventsRegistered.iterator();
			while(it2.hasNext()){
				Event ev2 = it2.next();
				if(ev1.getEventID() != ev2.getEventID()){
					eventsNotRegistered.add(ev1);
				}
			}
		}*/
		
		
		session.setAttribute("eventsRegisteredList", eventsRegistered);
		//session.setAttribute("eventsList", eventsNotRegistered);
		session.setAttribute("eventsList", events);
		
		
		/**
		 * Invokes the action "personalvisitor.jsp"
		 */
		RequestDispatcher rd = request.getRequestDispatcher("personalvisitor.jsp");
		rd.forward(request, response);
		
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
	}

}
